﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using asMeishi.web.Models;

namespace asMeishi.web.Controllers
{
    using Classes;
    public class impressionController : Controller
    {



        //
        // GET: /maroComment/Details/5

        public ActionResult Detail(int id)
        {
            return View();
        }

        //
        // GET: /maroComment/Create

        public ActionResult Create()
        {
            return View();
        } 

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">shopId</param>
        /// <returns></returns>
        [OutputCache(Duration = 30)]
        public ActionResult get(string id)
        {   //id:shopId
            //
            if (string.IsNullOrWhiteSpace(id)) return Content("");
            impressionC<shop> imc = new impressionC<shop>();
            var data = imc.getByTargetId(id.toOId()).Select(d =>
                new { Id=d.Id.ToString(), d.datetime, d.content, userId=d.userId.ToString(), userName = userC.userName(d.userId) }
                ).AsEnumerable();

            return json(data);
        }

        public ActionResult delByShopId(string id)
        {   //id:shopId
            impressionC<shop> imc = new impressionC<shop>();
            imc.delByShopId(id).ToString();

            //new historyC<shop>(id).add("picUrlDict", word);
            return Content("true");
        }
        public ActionResult del(string id,string word)
        {   //id:shopId
            impressionC<shop> imc = new impressionC<shop>();
            var imp = imc.delById(id);

            if (imp != null)
            {
                historyC<shop>.Add(id.toOId(), "impression", historyTypeEnum.删除, imp.content, userC.loginUser);
            }

            return Content("true");
        }
        public ActionResult delByShopIdByContent(string id, string word)
        {   //id:shopId
            impressionC<shop> imc = new impressionC<shop>();
            bool x = imc.delByShopIdByContent(id.toOId(), word);

            if (x)
            {
                historyC<shop>.Add(id.toOId(), "impression", historyTypeEnum.删除, word, userC.loginUser);
            }

            return Content("true");
        }
        public ActionResult getById(string id)
        {
            return View();
        }
        [HttpPost]
        public ActionResult add(FormCollection fc)
        {

            impressionC<shop> imc = new impressionC<shop>();
            impression impression =imc.create(fc["targetId"].toOId(),userC.loginUser);
            try
            {
                impression.datetime = DateTime.Now;
                impression.content = fc["content"];
                impression.grade = Convert.ToInt32(fc["grade"]);
                impression = imc.add(impression);
            }catch(Exception e){
                new asMeishiException("impression出错："+e);
            }

            var jsonImp = new { Id = impression.Id.ToString(), userId = impression.userId.ToString(), userName = userC.userName(impression.userId), content = impression.content, datetime = impression.datetime };

            return json(jsonImp);
        }
        private ActionResult json(object obj)
        {
            return Json(obj, JsonRequestBehavior.AllowGet);
            //return base.Content(obj.ToJSON());
        }
    }
}
